#include <Uefi.h>
#include <Base.h>
#include <Library/UefiLib.h>
#include <Library/BaseLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/PrintLib.h>
#include <Protocol/DiskIo.h>
#include <Protocol/BlockIo.h>
#include <Protocol/DevicePath.h>
#include <Protocol/DevicePath.h>
#include <Protocol/DevicePathToText.h>
#include <Uefi/UefiGpt.h>
#include <Library/DevicePathLib.h>

EFI_STATUS EFIAPI
UefiMain(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable)
{
    EFI_STATUS Status;
    UINTN HandleCount;
    EFI_HANDLE *DiskControllerHandles = NULL;
    EFI_DISK_IO_PROTOCOL *DiskIo;

    Status = gBS->LocateHandleBuffer(ByProtocol, &gEfiDiskIoProtocolGuid, NULL, &HandleCount, &DiskControllerHandles);
    if (EFI_ERROR(Status)) {

        Print("gBS->LocateHandleBuffer Failed!! [%d]\n", __LINE__);
        return Status;
    }

    CHAR8 gptHeadrSector[512];
    EFI_PARTITION_TABLE_HEADER * gptHeader = (EFI_PARTITION_TABLE_HEADER*)gptHeadrSector;

    for (UINTN HandleIndex = 0; HandleIndex < HandleCount; HandleIndex++) {

        Status = gBS->HandleProtocol(DiskControllerHandles[HandleIndex], &gEfiDiskIoProtocolGuid, (VOID**)&DiskIo);
        if (EFI_ERROR(Status)) {

            Print(" gBS->HandleProtocol Failed!!! [%d]\n", __LINE__);
            gBS->FreePool(DiskControllerHandles);
            return Status;
        }

        EFI_DEVICE_PATH_PROTOCOL *DiskDevicePath = NULL;
        EFI_DEVICE_PATH_TO_TEXT_PROTOCOL *Device2TextProtocol = NULL;
        CHAR16 *TextDevicePath = NULL;

        Status = gBS->OpenProtocol(DiskControllerHandles[HandleIndex], &gEfiDevicePathProtocolGuid, (VOID**)&DiskDevicePath, ImageHandle, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
        if (EFI_ERROR(Status)) {

            Print("gBS->OpenProtocol Failed!!! [%d]\n", __LINE__);
            gBS->FreePool(DiskControllerHandles);
            return Status;
        }

        Status = gBS->LocateProtocol(&gEfiDevicePathToTextProtocolGuid, NULL, (VOID**)&Device2TextProtocol);
        if (EFI_ERROR(Status)) {

            Print("gBS->LocateHandle Failed!!! [%d]\n", __LINE__);
            gBS->FreePool(DiskControllerHandles);
            return Status;
        }

        TextDevicePath = Device2TextProtocol->ConvertDevicePathToText(DiskDevicePath, TRUE, TRUE);
        Print(L"TextDevicePath = %s\n", TextDevicePath);

        if (TextDevicePath) {

            gBS->FreePool(TextDevicePath);
        }

        gBS->CloseProtocol(DiskControllerHandles[HandleIndex], &gEfiDevicePathProtocolGuid, ImageHandle, NULL);
    }

    return Status;
}
